/**
 * 中间的内容页面公共js
 */

$(function () {
    var parentDoc = window.parent.document;
    $(document).on("click", function (e) {
        var isTarget = $(e.target).closest("#navChange2", parentDoc).length;
        if (isTarget) {
            $("#cateNav", parentDoc).slideDown();
        } else {
            $("#cateNav", parentDoc).slideUp();
            $("#split", parentDoc).show();
        }
    });

    /* 高级搜索展开收起 */
    if ($("#advActBtn").length) {
        $("#advActBtn").click(function () {
            var actAdv = $("#actAdv");
            var isHidden = actAdv.is(":hidden");
            if (isHidden) {
                actAdv.show(function () {
                    var t = actAdv.position().top;
                    actAdv.closest(".x-panel-body-default").animate({scrollTop: t - 4}, 300);
                    $("#advActBtn").removeClass("btn-primary");
                });
            } else {
                actAdv.slideUp();
                $("#advActBtn").addClass("btn-primary");
            }
        });
    }

    /* 头部表单编辑区域 - 收起展开 */
    if ($(".action-area").length) {
        $(".action-area").each(function () {
            $(this).on("click", "dt", function () {
                var dd = $(this).next("dd");
                var isVisible = dd.is(":visible");
                if (isVisible) {
                    dd.slideUp();
                    $(this).find(".arrow").removeClass("arrow-on");
                } else {
                    dd.slideDown();
                    $(this).find(".arrow").addClass("arrow-on");
                }
            });

            var dt = $(this).find("dt");
            $(this).on("click", ".dg-wrap-expander", function () {
                dt.click();
            });
        });
    }

    /* 折叠头部搜索 */
    if ($(".bd-editbar").length) {
        var edBar = $(".bd-editbar");
        edBar.on("click", ".dg-wrap-expander", function () {
            var edbarForm = $(this).siblings("form");
            var isHidden = edbarForm.is(":hidden");
            if (!isHidden) {
                edbarForm.slideUp();
                $(this).removeClass("dg-wrap-collapse").addClass("dg-wrap-expand");
            } else {
                edbarForm.slideDown();
                $(this).removeClass("dg-wrap-expand").addClass("dg-wrap-collapse");
            }
        });
    }

    /* 数据详细表格下的easyui-tab自适应宽度 */
    if ($("#dgDetail").length) {
        $("#dgDetail").tabs({
            width: $(this).parent().width(),
            height: "auto"
        });
    }

    if ($("#cc").length) {
        //上下分栏的折叠显隐
        $("#cc").layout("panel", "south").find(".dg-wrap-expander").toggle(function () {
            $(this).removeClass("dg-wrap-expand").addClass("dg-wrap-collapse");
            $("#cc").layout("panel", "south").panel("resize", {height: 10});
            $("#cc").layout("resize");
        }, function () {
            $(this).removeClass("dg-wrap-collapse").addClass("dg-wrap-expand");
            setDgLayout();
        });
        //将分栏折叠按钮移动为 .layout-panel-south 的直接子元素
        var southPanel = $("#cc").layout("panel", "south");
        var detailDgWrap = southPanel.find(".detail-dg-wrap").parent();
        southPanel.find(".dg-wrap-expander").insertBefore(detailDgWrap);
    }
});

window.onload = function () {
    window.parent.document.title = document.title;
};

/* 改造后的分页 */
function pageTotalLoad(url, gridId) {
    $.fn.pagination.defaults.onPageAjaxLoad = function () {
        $(gridId).datagrid('options').url = url + '?total=1';
        $(gridId).datagrid('reload2');
        setTimeout(function () {
            $(gridId).datagrid('options').url = url;
        }, 0);
    }
}

/* 点击隐藏头部表单编辑区域 */
function clickHide() {
    if ($("#actEdit").find("dd").is(":visible")) {
        $("#actEdit").find("dt").click();
    }
}

/* 点击显示头部表单编辑区域 */
function clickShow() {
    if ($("#actEdit").find("dd").is(":hidden")) {
        $("#actEdit").find("dt").click();
    }
}

/* 设置分栏初始高度为7：3 */
function setDgLayout() {
    var cc = $("#cc");
    var southPanel = cc.layout("panel", "south");   // .layout-panel-south > .layout-body
    var winH = $(window).height();
    var crumbH = $(".breadcrumb").outerHeight();
    var ccH = winH - crumbH;
    cc.height(ccH);
    southPanel.panel('resize', {height: ccH * 0.3});
    $("#cc").layout("resize");
}

/* 数据表格加载成功后触发 */
function autoResizeDgHeight() {
    var centerPanel = $("#cc").layout("panel", "center");   // .layout-panel-center > .layout-body
    var dataGrid = centerPanel.children(".datagrid");
    var dgWrap = dataGrid.find(".datagrid-wrap");
    var dgView = dataGrid.find(".datagrid-view");
    var pager = dataGrid.find(".datagrid-pager");
    var dgBTable = dataGrid.find(".datagrid-btable");
    var pagerH = pager.outerHeight();
    var centerH = centerPanel.height();     // center panel的初始高度

    /* 获取center pannel中，去掉第一个和最后一个子元素的总高度 */
    var centerChild = centerPanel.children();
    var centerChildH = 0;
    var centerChildLen = centerChild.length;
    for (var i = 0; i < (centerChildLen - 1); i++) {
        centerChildH += centerChild.eq(i).outerHeight();
    }
    /* 分栏高度改变时，动态改变表格高度 */
    centerPanel.panel({
        onResize: function () {
            var $this = $(this);    // centerPanel
            var centerH = $this.height();
            if (centerH > (centerChildH + pagerH + 24 + 60)) {    // 24为预定义滚动条高度，60为表格最小高度
                dgWrap.height(centerH - centerChildH);
                dgView.height(centerH - centerChildH);
                dataGrid.find(".datagrid-body").height(centerH - centerChildH - pagerH - 24);
            } else {
                dgWrap.height(centerH);
                dgView.height(centerH);
                dataGrid.find(".datagrid-body").height(centerH - pagerH - 24);
            }
        }
    });

    $("#cc").layout("resize");
}

/* 点击每行表格时，记住上次选择的明细tab标签 */
function getTabSelect() {
    var pp = $("#dgDetail").tabs('getSelected');
    var tab = pp.panel('options').tab;
    $("#dgDetail").tabs("select", tab);
}

//加载中。。。处理进行中
function Load() {
    $("<div class=\"datagrid-mask\"></div>").css({ display: "block", width: "100%", height: $(window).height() }).appendTo("body");
    $("<div class=\"datagrid-mask-msg\"></div>").html("正在运行，请稍候。。。").appendTo("body").css({ display: "block", left: ($(document.body).outerWidth(true) - 190) / 2, top: ($(window).height() - 45) / 2 });
}

//删除加载中。。。
function dispalyLoad() {
    $(".datagrid-mask").remove();
    $(".datagrid-mask-msg").remove();
}